package com.hsogoo.avatar.serialization.json;

import java.io.IOException;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.hsogoo.avatar.serialization.Serializer;

/**
 * @Author huangsaigang
 * Created @2018/9/25.
 */
public class JsonSerializer implements Serializer {

    private SerializerFeature[] features = {
            SerializerFeature.WriteClassName,
    };

    @Override
    public byte[] encode(Object msg) throws IOException {
        return JSON.toJSONString(msg,features).getBytes("utf-8");
    }

    @Override
    public <T> T decode(byte[] buf, Class<T> type) throws IOException {
        String jsonString = new String(buf,"utf-8");
        Object result = JSON.parseObject(jsonString, type);
        return (T)result;
    }

}
